package ylsb.service;


import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import ylsb.dao.Hc2Mapper;
import ylsb.dao.Hc3Mapper;
import ylsb.dao.ShijiMapper;
import ylsb.model.*;
import ylsb.support.JsonResult;
import ylsb.support.PageInfo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Hc3Service {

    @Resource
    private Hc3Mapper ddMapper;

    private static int startindex = 0;

    public List<Map<String, Object>> findPinpaiAll(String ppxiangmuId){
        return ddMapper.findPinpaiAll(ppxiangmuId);
    }
//    public List<Map<String, Object>> findGuochanPinzhong(String ppxiangmuId){
//        return ddMapper.findGuochanPinzhong(ppxiangmuId);
//    }
//    public List<Map<String, Object>> findFengbi(String ppxiangmuId){
//        return ddMapper.findFengbi(ppxiangmuId);
//    }
//    public List<Map<String, Object>> findFengbiCount(String ppxiangmuId,String ppPinzhong){
//        return ddMapper.findFengbiCount(ppxiangmuId,ppPinzhong);
//    }

//    public List<Map<String, Object>> findGuochanBypinzhong(String ppxiangmuId,String ppPinzhong){
//        return ddMapper.findGuochanBypinzhong(ppxiangmuId,ppPinzhong);
//    }
//    public List<Map<String, Object>> findResultBypinzhong(String ppxiangmuId, String ppuserId,String ppPinzhong,String ppPinpai){
//        return ddMapper.findResultBypinzhongpinpai(ppxiangmuId,ppuserId,ppPinzhong,ppPinpai);
//    }

//    public List<Map<String, Object>> findResultGuochanBypinzhong(String ppxiangmuId, String ppuserId,String ppPinzhong){
//        return ddMapper.findResultGuochanBypinzhong(ppxiangmuId,ppuserId,ppPinzhong);
//    }

//    public List<Map<String, Object>> findAllByXiangmuIdAndTiaojian(String xiangmuId,String mmleibie,String mmpinpaiqingkuang){
//        return ddMapper.findAllByXiangmuIdAndTiaojian(xiangmuId,mmleibie,mmpinpaiqingkuang);
//    }

    public List<Map<String, Object>> findXinghaoByPinpai(String ppxiangmuId,String ppPinzhong,String ppPinpai){
        return ddMapper.findXinghaoByPinpai(ppxiangmuId,ppPinpai);
    }

//    public Integer getPinguiCount(String mmxiangmuId){
//        Object mmCount = ddMapper.getPinguiCount(mmxiangmuId);
//        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
//    }
//    public Integer getPinzhongCount(String mmxiangmuId){
//        Object mmCount = ddMapper.getPinzhongCount(mmxiangmuId);
//        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
//    }
//    public Integer getPaichuPinguiCount(String mmxiangmuId){
//        Object mmCount = ddMapper.getPaichuPinguiCount(mmxiangmuId);
//        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
//    }

    public Hc3 findOne(String id){
        return ddMapper.selectByPrimaryKey(id);
    }

//    public List<Map<String,Object>> getCountByPinzhongAndPinpai(int mmPinzhong,String mmPinpai,String mmxiangmuId){
//        return ddMapper.getCountByPinzhongAndPinpai(mmPinzhong,mmPinpai,mmxiangmuId);
//    }

    public JsonResult<?> findhc3list(String ppxiangmuId, int ppPageIndex, int ppPageSize) {

        List<Hc3pinpai> hc3pinpais = ddMapper.selectAllByXiangmuid(ppxiangmuId, (ppPageIndex - 1) * ppPageSize, ppPageSize);
        List<Map<String,Object>> hc3List = new ArrayList<>();
        for (Hc3pinpai pinpai : hc3pinpais) {
            Map<String, Object> pinpaiMap = new HashMap<>();
            int size = pinpai.getHc3xinghaoList().size();
            pinpaiMap.put("pinpaixuhao", pinpai.getPinpaixuhao());
            pinpaiMap.put("shebeimingcheng", pinpai.getShebeimingcheng() != null ? pinpai.getShebeimingcheng() : "");
            pinpaiMap.put("shebeipinpai", pinpai.getShebeipinpai() != null ? pinpai.getShebeipinpai() : "");
            pinpaiMap.put("shebeixinghao", pinpai.getShebeixinghao() != null ? pinpai.getShebeixinghao() : "");
            pinpaiMap.put("xinghaolist", pinpai.getHc3xinghaoList());
            pinpaiMap.put("pinpaihebingshu", size == 0 ? 1 : size);
            hc3List.add(pinpaiMap);
        }
        int count = getHc3list(ppxiangmuId, ppPageIndex, ppPageSize);
        PageInfo pageInfo = new PageInfo(ppPageIndex, ppPageSize, count);

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("Hc3List", hc3List);
        returnMap.put("PageInfo", pageInfo);

        return JsonResult.getSuccessResult(returnMap);
    }

    public JsonResult<?> findBiaojuelist(@RequestParam("xiangmuid") String ppxiangmuId,
                                         @RequestParam("userid") String ppuserId,
//                                         @RequestParam("tiaojian") String pptiaojian,
                                         @RequestParam("pageindex") int ppPageIndex,
                                         @RequestParam("pagesize") int ppPageSize) {
//        String mmleibie="";
//        String mmpinpaiqingkuang="";
//        if(pptiaojian.equals("封闭试剂")){
//            mmleibie=pptiaojian;
//        }else if(pptiaojian.equals("一个品牌")){
//            mmpinpaiqingkuang=pptiaojian;
//        }else if(pptiaojian.equals("两个品牌")){
//            mmpinpaiqingkuang=pptiaojian;
//        }else if(pptiaojian.equals("专机专用")){
//            mmpinpaiqingkuang=pptiaojian;
//        }


        List<Hc3pinpai> pinpaiAll = ddMapper.selectBiaojueByXiangmuid(ppxiangmuId, ppuserId, 0, 9999);
        List<Map<String,Object>> hc3List = new ArrayList<>();

        int startindex = 0;
        if (ppPageIndex != 1){
            for (int i = 0; i < (ppPageIndex - 1) * ppPageSize; i++) {
                if (i >= pinpaiAll.size())
                    break;
                startindex += pinpaiAll.get(i).getHc3xinghaoList().size();
            }
        }
        int pageSize = 0;
        for (int i = (ppPageIndex - 1) * ppPageSize; i < ppPageIndex * ppPageSize; i++) {
            if (i >= pinpaiAll.size())
                break;
            pageSize += pinpaiAll.get(i).getHc3xinghaoList().size();
        }
        List<Hc3pinpai> pinpais = ddMapper.selectBiaojueByXiangmuid(ppxiangmuId, ppuserId, startindex, pageSize);

        for (Hc3pinpai pinpai : pinpais) {
            Map<String, Object> pinpaiMap = new HashMap<>();
            int size = pinpai.getHc3xinghaoList().size();
            pinpaiMap.put("pinpaixuhao", pinpai.getPinpaixuhao());
            pinpaiMap.put("shebeimingcheng", pinpai.getShebeimingcheng() != null ? pinpai.getShebeimingcheng() : "");
            pinpaiMap.put("shebeipinpai", pinpai.getShebeipinpai() != null ? pinpai.getShebeipinpai() : "");
            pinpaiMap.put("shebeixinghao", pinpai.getShebeixinghao() != null ? pinpai.getShebeixinghao() : "");
            pinpaiMap.put("xinghaolist", pinpai.getHc3xinghaoList());
            pinpaiMap.put("pinpaihebingshu", size == 0 ? 1 : size);
            hc3List.add(pinpaiMap);
        }

        int count = getHc3list(ppxiangmuId, ppPageIndex, ppPageSize);
        PageInfo pageInfo = new PageInfo(ppPageIndex, ppPageSize, count);

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("Hc3List", hc3List);
        returnMap.put("PageInfo", pageInfo);

        return JsonResult.getSuccessResult(returnMap);
    }

    public Integer getHc3list(String ppxiangmuId,int ppPageIndex, int ppPageSize) {
        Object mmCount = ddMapper.getHc3list(ppxiangmuId,ppPageIndex,ppPageSize);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public boolean add (Hc3 hc45){
        return ddMapper.insert(hc45)==1;
    }

    public boolean isexistByxiangmuId(String xiangmuId){
        Hc3Example mmExample=new Hc3Example();
        Hc3Example.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId);
        return  ddMapper.selectByExample(mmExample).size()>0;
    }

    public boolean deleteByxiangmuId(String xiangmuId){
        Hc3Example mmExample=new Hc3Example();
        Hc3Example.Criteria criteria=mmExample.createCriteria();
        criteria.andXiangmuidEqualTo(xiangmuId);
        return  ddMapper.deleteByExample(mmExample)>=0;
    }
}
